//
//  PublicTextAndPicTableViewCell.m
//  AAIM
//
//  Created by 远的小布丁 on 2020/1/25.
//  Copyright © 2020 AAIM. All rights reserved.
//

#import "PublicTextAndPicTableViewCell.h"

@implementation PublicTextAndPicTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _cellType = reuseIdentifier;
        self.backgroundColor = KHEXCOLOR(@"F4F4F4");
        [self setUI];
    }
    return self;
}
- (void)setCellType:(NSString *)cellType{
    _cellType = cellType;
}
- (void)setUI{
    if ([_cellType isEqualToString:@"publicTextContent"]) {
        _headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth-45, 10, 30, 30)];
        _headerImg.layer.cornerRadius = 15;
        _headerImg.layer.masksToBounds = YES;
        _headerImg.image = [UIImage imageNamed:@"我的-头像"];
        [self.contentView addSubview:_headerImg];
        
        _contentLbl = [[UILabel alloc]initWithFrame:CGRectMake(45, 10, kScreenWidth-90, 30)];
        _contentLbl.textAlignment = 2;
        _contentLbl.font = kMainFont(15);
        _contentLbl.textColor = KHEXCOLOR(AllBackColor_titleColor);
        _contentLbl.text = @"每天分享国内外湖两旺与投资消息速递，第一时间与您分享，欢迎关注";
        _contentLbl.numberOfLines = 0;
        [self.contentView addSubview:_contentLbl];
    }else if ([_cellType isEqualToString:@"publicPicContent"]){
        _headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth-45, 10, 30, 30)];
        _headerImg.layer.cornerRadius = 15;
        _headerImg.layer.masksToBounds = YES;
        _headerImg.image = [UIImage imageNamed:@"我的-头像"];
        [self.contentView addSubview:_headerImg];
        
        _contentImg = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth-155.f, self.headerImg.top, 100, 100*671/375.f)];
        _contentImg.image = [UIImage imageNamed:@"客服中心背景"];
        _contentImg.layer.cornerRadius = 5;
        _contentImg.layer.masksToBounds = YES;
        [self.contentView addSubview:_contentImg];
    }
    
    
}

- (void)setModel:(ChildMenusModel *)model{
    KWeakSelf;
    if ([_cellType isEqualToString:@"publicTextContent"]) {
        if (model.messageDirection == 0) {
            _headerImg.frame = CGRectMake(kScreenWidth-45, 10, 30, 30);
            [_headerImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[LoginManager shareLoginManager].userModel.head]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                weakSelf.headerImg.image = image;
            }];
            
            _contentLbl.text = model.describe;
            _contentLbl.frame = CGRectMake(45, 10, kScreenWidth-100, [self calculationCellHeight:_contentLbl]);
        }else{
            _headerImg.frame = CGRectMake(15, 10, 30, 30);
            [_headerImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[LoginManager shareLoginManager].userModel.head]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                weakSelf.headerImg.image = image;
            }];
            _contentLbl.text = model.describe;
            _contentLbl.frame = CGRectMake(_headerImg.right+10, 10, kScreenWidth-100, [self calculationCellHeight:_contentLbl]);
        }
    }else if ([_cellType isEqualToString:@"publicPicContent"]){
        if (model.messageDirection == 0) {
            _headerImg.frame = CGRectMake(kScreenWidth-45, 10, 30, 30);
            [_headerImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[LoginManager shareLoginManager].userModel.head]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                weakSelf.headerImg.image = image;
            }];
            [_contentImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.pic]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                weakSelf.contentImg.image = image;
            }];
            _contentImg.frame = CGRectMake(kScreenWidth-155.f, self.headerImg.top, 100, 100*671/375.f);
        }else{
           _headerImg.frame = CGRectMake(15, 10, 30, 30);
            [_headerImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,model.head]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                weakSelf.headerImg.image = image;
            }];
            [_contentImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.pic]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                           weakSelf.contentImg.image = image;
                       }];
            _contentImg.frame = CGRectMake(self.headerImg.right+10, self.headerImg.top, 100, 100*671/375.f);
        }
    }
}

- (CGFloat)calculationCellHeight:(UILabel *)contentLbl{
    
    CGRect rect = [contentLbl.text boundingRectWithSize:CGSizeMake(contentLbl.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: kMainFont(15)} context:nil];
    return rect.size.height;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
